﻿
//关闭当前页 返回父页面
function closePage(isReload) {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //再执行关闭
    if (isReload) {
        parent.search();
    }
}

//初始化角色
function initRole() {
    $('#tbRoleGUID').selectPage({
        data: '/Business/Common/Common.ashx?PostType=list&ActionType=Roles',
        showField: 'RoleName',
        keyField: 'GUID',
        //查询过滤字段
        searchField: 's_0_RoleName',
        //是否分页
        pagination: false,
        //多选模式
        multiple: true,
        //是否启用多选模式的控制按钮区域，仅multiple: true模式下可用
        multipleControlbar: false,
        //格式化显示项目，提供源数据进行使用
        formatItem: function (data) {
            return data.RoleName;
        },
        panelWidth: 343,
        //选中项目后的回调处理
        //data：选中行的原始数据对象
        eSelect: function (data) {
        },
        //项目移除后的回调
        //datas: 被移除的项目(Array)
        eTagRemove: function (datas) {
        },
        //ajax请求后服务端返回的数据格式处理
        //返回的数据里必须包含list（Array）和totalRow（number|string）两个节点
        eAjaxSuccess: function (d) {
            return d;
        }
    });
}

layui.use(['form', 'layer', 'laydate', 'upload'], function () {
    var form = layui.form,
        layer = layui.layer,
        laydate = layui.laydate,
        upload = layui.upload;

    var portraitImageUrl = '/Business/Common/UploadifyHandler.ashx?PostType=upload&RecordType=PortraitPicturesTemp';

    //上传身份证头像
    upload.render({
        elem: '#tbUploadImageUrl'
        , url: portraitImageUrl
        , accept: 'images'
        , acceptMime: 'image/*'
        , exts: 'jpg|png|gif'
        //, size: 50
        , before: function (obj) {
            layer.load(); //上传loading
        }
        , done: function (res, index) {
            layer.closeAll('loading');
            if (res.AnnexPath) {
                $('#tbPortraitUrl').val(res.AnnexPath);
                $('#jpgfile').attr('src', res.AnnexPath + '?rnd=' + Math.random());
            }
        }
        , error: function (index, upload) {
            layer.closeAll('loading');
        }
    });

    $(function () {
        if (app.getRequest('GUID')) {
            initData();
        } else {
            initRole();
        }
    });

    //初始化表单
    function initData() {
        var fieldData = {
            GUID: app.getRequest('GUID'),
            PostType: "get",
            ActionType: "UserInfo"
        };
        app.loadData("UserInfoManage.ashx", fieldData, function (data) {
            $('#tbRoleGUID').val(data.RoleGUID);
            $('#tbUserType').val(data.UserType);
            $('#tbIsLock').val(data.IsLock);

            if (data.PortraitUrl) {
                $("#jpgfile").attr('src', data.PortraitUrl + '?t=' + Math.random());
            }

            initRole();
            form.render();
        }, function (err) {
            layer.msg('请求失败');
        });
    }

    //保存
    form.on('submit(save)', function (data) {
        //if (!$('#tbPortraitUrl').val()) {
        //    layer.msg('请上传头像！');
        //    return false;
        //}

        var fieldData = data.field;
        fieldData["GUID"] = app.getRequest('GUID');
        fieldData["ActionType"] = "UserInfo";
        app.set("UserInfoManage.ashx", fieldData, function (data) {
            layer.msg(data.msg);
            if (data.success) {
                setTimeout(function () {
                    closePage(true);
                }, 1000);
            }
        }, function (err) {
            layer.msg('请求失败');
        });
        return false;
    });
});
